<?php
	include("../include/init.php");//将配置文件全部加载进来
	include("../include/check_login.php");//登录

	$page = new sql_class("product");

	$current = isset($_GET['page']) ? $_GET['page'] : 1;//当前页数

	//单条删除
	$product_id = isset($_GET['product_id']) ? $_GET['product_id'] : 0;
	if($product_id){
		$delete_id = $page->delete("product_id = $product_id");
		if($delete_id){
			show_msg("删除产品成功","pro_list.php");
			exit;
		}else{
			show_msg("删除产品失败","pro_list.php");
			exit;
		}
	}

	//删除多条
	$delete_more = isset($_GET['id']) ? implode(",",$_GET['id']) : 0;
	if($delete_more){
		$where = "product_id IN($delete_more)";
		$delete = $page->delete($where);
		if($delete){
			show_msg("批量删除产品成功","pro_list.php");
		}else{
			show_msg("批量删除产品失败","pro_list.php");
		}
	}

	//搜索
	$start_time = isset($_GET['start_time']) ? $_GET['start_time'] : 0;//获取开始时间
	$end_time = isset($_GET['end_time']) ? $_GET['end_time'] : 0;//获取结束时间
	$keywords = isset($_GET['keywords']) ? trim($_GET['keywords']) : 0;//获取搜索框中的内容

	$start_time = strtotime($start_time);
	$end_time = strtotime($end_time);
	
	$where = "";
	if($start_time && !$end_time){
		$where = "product_time > $start_time";
	}else if(!$start_time && $end_time){
		$where = "product_time < $end_time";
	}else if($start_time && $end_time){
		$where = "product_time BETWEEN $start_time AND $end_time";
	}else if(!empty($keywords)){
		$where ="product_name = '$keywords'";
	}else{
		$where = 1;
	}

	//分页
	$limit = 6;
	$size = 4;

	$count = $page->select($where,"COUNT(*) AS c")->getOne();
	$start = ($current - 1)*$limit;

	$page_str = page($current,$count['c'],$limit,$size);
	$pro_list = $page->select($where)->orderBy(['product_id','DESC'])->limit([$start,$limit])->getAll();


	//传值到html中
	$smarty->assign("start_time",$start_time);
	$smarty->assign("end_time",$end_time);
	$smarty->assign("page_str",$page_str);
	$smarty->assign("pro_list",$pro_list);
	$smarty->display("admin/pro_list.html");//连接index.html



?>